--[[
* MapLib 2.0
* File: <AddOn>\MapLib\MapLib_WindowMode.lua
* $Revision: 132 $
* $Date: 2007-01-06 13:08:56 +0000 (Sat, 06 Jan 2007) $
* 
* Copyright (c) 2006-2007, Mery (Moritz Metelmann)
* All rights reserved.
* 
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* 
*    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*    * Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
]]--

local vmajor, vminor = "MapLib-2.0", tonumber(string.sub("$Revision: 132 $", 12, -3))

-- Version check: Only one major instance of MapLib can be installed, if another already is: abort.
if (MapLib and MapLib.WindowMode and (MapLib.WindowMode.vmajor ~= vmajor or MapLib.WindowMode.vminor >= vminor)) then return; end;

if (not MapLib) then MapLib = {}; end;
if (not MapLib.WindowMode) then MapLib.WindowMode = {}; end;
MapLib.WindowMode.vmajor = vmajor;
MapLib.WindowMode.vminor = vminor;

MapLib.WindowMode.MIN_SCALE = 0.3;
-- Disable WindowMode because I don't feel like fixing the bugs at the moment.
MapLib.WindowMode.disabled = true;

MapLib.WindowMode.VariablesLoaded = function()
	-- Start: DISABLED WINDOW MODE!!
	-- Hide the window mode button and the resizing button.
	if (MapLib.WindowMode.disabled) then
		MapLibFrameWindowedButton:Hide();
		MapLibFrameSizingButton:Hide();
		return nil;
	end;
	-- Stop: DISABLED WINDOW MODE!!
	-- check if there already is a window profile, if not load defaults
	if (not MapLibSaved) then MapLibSaved = {}; end;
	if (not MapLibSaved.WindowMode) then MapLibSaved.WindowMode = {
		anchor = "TOPLEFT",
		relativeTo = nil,
		relativePoint = "TOPLEFT",
		xOffset = 100,
		yOffset = -100,
		scale = 0.7,
		isWindowed = false,
		locked = false,
	}; end;
	-- set mode if it was windowed
	if (MapLibSaved.WindowMode.isWindowed) then
		MapLib.WindowMode.SetWindowed(true);
	end;
end;

--~ DevTools_Dump("WindowMode file loaded");

MapLib.WindowMode.SetWindowed = function(forceHidden)
	UIPanelWindows["MapLibFrame"] = nil;
	MapLibFrame:SetFrameStrata("HIGH");
	
	MapLib.WindowMode.SetLocked(MapLibSaved.WindowMode.locked);

	MapLibFrame:SetMovable(true);
	MapLibFrame:SetResizable(true);
	MapLibFrame:RegisterForDrag("LeftButton");
	MapLibFrame:SetScript("OnDragStart", function()
		this:StartMoving();
		MapLibFrame:SetClampedToScreen(true);
	end)
	MapLibFrame:SetScript("OnDragStop", function()
		this:StopMovingOrSizing();

		-- Remeber the position? yeah you should do that.
		local save = MapLibSaved.WindowMode;
		save.anchor, save.relativeTo, save.relativePoint, save.xOffset, save.yOffset = MapLibFrame:GetPoint();
		local resolution_x, resolution_y = string.match(GetCVar("gxResolution"), "(%d+)x(%d+)");
		if (save.xOffset > resolution_x / MapLibFrame:GetScale() - 1024) then save.xOffset = resolution_x / MapLibFrame:GetScale() - 1024; end;
		if (- save.yOffset > resolution_y / MapLibFrame:GetScale() - 768) then save.yOffset = -(resolution_y / MapLibFrame:GetScale() - 768); end;
		if (save.xOffset < 0) then save.xOffset = 0; end;
		if (save.yOffset > 0) then save.yOffset = 0; end;
		MapLibFrame:SetPoint(save.anchor, save.relativeTo, save.relativePoint, save.xOffset, save.yOffset);
		MapLibFrame:SetClampedToScreen(false);
	end)

	MapLibFrame:ClearAllPoints();
	MapLibFrame:SetPoint(MapLibSaved.WindowMode.anchor, MapLibSaved.WindowMode.relativeTo, MapLibSaved.WindowMode.relativePoint, MapLibSaved.WindowMode.xOffset, MapLibSaved.WindowMode.yOffset);
	MapLibFrame:SetScale(MapLibSaved.WindowMode.scale);
	MapLibFrame:SetWidth(1024);
	MapLibFrame:SetHeight(768);
	MapLibBlackoutWorldTexture:Hide();
	
 	MapLibFrameMinimizeButton:Show();
 	MapLibFrameLockButton:Show();
 	MapLibFrameSpeedButton:Show();
	
	MapLibFrameWindowedButton:SetNormalTexture(MapLib.PATH.."\\Artwork\\Button-Fullscreen-Up");
	MapLibFrameWindowedButton:SetPushedTexture(MapLib.PATH.."\\Artwork\\Button-Fullscreen-Down");
	
	MapLibFrame.isInWindowMode = true;
	MapLibSaved.WindowMode.isWindowed = true;
	MapLib.SideMenu.SetWindowedMode();
	HideUIPanel(MapLibFrame);
--~ 	MapLibFrame:SetParent("UIParent");
	if (not forceHidden) then ShowUIPanel(MapLibFrame); end;
end;

MapLib.WindowMode.SetFullscreen = function(forceHidden)
	UIPanelWindows["MapLibFrame"] = { whileDead = 1, pushable = 0, area = "full" };
	MapLibFrame:SetFrameStrata("FULLSCREEN");

	MapLib.WindowMode.SetLocked(true);

	MapLibFrame:SetMovable(flase);
	MapLibFrame:SetResizable(flase);
	MapLibFrame:SetScript("OnDragStart", nil);
	MapLibFrame:SetScript("OnDragStop", nil);

	MapLibFrame:SetScale(1);
	MapLibFrame:SetAlpha(1);

	MapLibFrame:ClearAllPoints();
	MapLibFrame:SetAllPoints(true);
	MapLibBlackoutWorldTexture:Show();
	
	MapLibFrameMinimizeButton:Hide();
	MapLibFrameLockButton:Hide();
	MapLibFrameSpeedButton:Hide();
	
	MapLibFrameWindowedButton:SetNormalTexture(MapLib.PATH.."\\Artwork\\Button-Windowed-Up");
	MapLibFrameWindowedButton:SetPushedTexture(MapLib.PATH.."\\Artwork\\Button-Windowed-Down");
	
	MapLibFrame.isInWindowMode = nil;
	MapLibSaved.WindowMode.isWindowed = false;
	MapLib.SideMenu.SetFullscreenMode();
	HideUIPanel(MapLibFrame);
--~ 	MapLibFrame:SetParent(nil);
	if (not forceHidden) then ShowUIPanel(MapLibFrame); end;
end;

MapLib.WindowMode.SetLocked = function(lock)
	if (lock) then
		MapLibFrame:EnableMouse(false);
		MapLibFrameLockButton:SetNormalTexture(MapLib.PATH.."\\Artwork\\Button-Locked-Up");
		MapLibFrameLockButton:SetPushedTexture(MapLib.PATH.."\\Artwork\\Button-Locked-Down");
		MapLibFrameSizingButton:Hide();
		MapLibFrame.isLocked = true;
	else
		MapLibFrame:EnableMouse(true);
		MapLibFrameLockButton:SetNormalTexture(MapLib.PATH.."\\Artwork\\Button-Unlocked-Up");
		MapLibFrameLockButton:SetPushedTexture(MapLib.PATH.."\\Artwork\\Button-Unlocked-Down");
		MapLibFrameSizingButton:Show();
		MapLibFrame.isLocked = false;
	end;
end;

MapLib.WindowMode.Toggle = function()
	if (MapLibFrame.isInWindowMode) then
		MapLib.WindowMode.SetFullscreen();
	else
		MapLib.WindowMode.SetWindowed();
	end;
end;